import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:hive_flutter/hive_flutter.dart';

import 'core/constants/app_constants.dart';
import 'core/themes/app_theme.dart';
import 'core/di/service_locator.dart';
import 'data/models/favorite_model.dart';
import 'presentation/providers/theme_provider.dart';
import 'presentation/providers/anime_provider.dart';
import 'presentation/providers/favorite_provider.dart';
import 'presentation/providers/content_filter_provider.dart';
import 'presentation/pages/splash_page.dart';
import 'presentation/pages/home_page.dart';
import 'presentation/pages/detail_page.dart';
import 'presentation/pages/search_page.dart';
import 'presentation/pages/favorites_page.dart';
import 'presentation/pages/settings_page.dart';
import 'presentation/pages/category_detail_page.dart';
import 'presentation/pages/ranking_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化Hive
  await Hive.initFlutter();
  
  // 注册Hive适配器
  Hive.registerAdapter(FavoriteModelAdapter());
  
  // 初始化依赖注入
  await setupServiceLocator();
  
  // 初始化ThemeProvider
  final themeProvider = ThemeProvider();
  await themeProvider.init();
  
  runApp(AniQuestApp(themeProvider: themeProvider));
}

class AniQuestApp extends StatelessWidget {
  final ThemeProvider themeProvider;
  
  const AniQuestApp({super.key, required this.themeProvider});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: themeProvider),
        ChangeNotifierProvider<ContentFilterProvider>(
          create: (_) => getIt<ContentFilterProvider>(),
        ),
        ChangeNotifierProvider<AnimeProvider>(
          create: (_) => getIt<AnimeProvider>(),
        ),
        ChangeNotifierProvider<FavoriteProvider>(
          create: (_) => getIt<FavoriteProvider>(),
        ),
      ],
      child: Consumer<ThemeProvider>(
        builder: (context, themeProvider, child) {
          return MaterialApp(
            title: AppConstants.appName,
            debugShowCheckedModeBanner: false,
            theme: AppTheme.lightTheme,
            darkTheme: AppTheme.darkTheme,
            themeMode: themeProvider.themeMode,
            initialRoute: RouteConstants.splash,
            routes: {
              RouteConstants.splash: (context) => const SplashPage(),
              RouteConstants.home: (context) => const HomePage(),
              RouteConstants.detail: (context) => const DetailPage(),
              RouteConstants.search: (context) => const SearchPage(),
              RouteConstants.favorites: (context) => const FavoritesPage(),
              RouteConstants.settings: (context) => const SettingsPage(),
              RouteConstants.ranking: (context) => const RankingPage(),
              RouteConstants.categoryDetail: (context) {
                final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
                return CategoryDetailPage(
                  categoryName: args['categoryName'],
                  categoryType: args['categoryType'],
                  categoryValue: args['categoryValue'],
                  categoryIcon: args['categoryIcon'],
                  categoryColor: args['categoryColor'],
                );
              },
            },
          );
        },
      ),
    );
  }
}
